home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS09.ADF / MicroEMACS / file.c < prev    next >
C/C++ Source or Header  |  1986-05-22  |  13KB  |  383 lines

  1. /*
  2.  * The routines in this file
  3.  * handle the reading and writing of
  4.  * disk files. All of details about the
  5.  * reading and writing of the disk are
  6.  * in "fileio.c".
  7.  */
  8. #include        <stdio.h>
  9. #include        "ed.h"
  10.  
  11. /*
  12.  * Read a file into the current
  13.  * buffer. This is really easy; all you do it
  14.  * find the name of the file, and call the standard
  15.  * "read a file into the current buffer" code.
  16.  * Bound to "C-X C-R".
  17.  */
  18. fileread(f, n)
  19. {
  20.         register int    s;
  21.         char            fname[NFILEN];
  22.  
  23.         if ((s=mlreply("Open file: ", fname, NFILEN)) != TRUE)
  24.                 return (s);
  25.         return (readin(fname));
  26. }
  27.  
  28. /*
  29.  * Select a file for editing.
  30.  * Look around to see if you can find the
  31.  * fine in another buffer; if you can find it
  32.  * just switch to the buffer. If you cannot find
  33.  * the file, create a new buffer, read in the
  34.  * text, and switch to the new buffer.
  35.  * Bound to C-X C-V.
  36.  */
  37. filevisit(f, n)
  38. {
  39.         register BUFFER *bp;
  40.         register WINDOW *wp;
  41.         register LINE   *lp;
  42.         register int    i;
  43.         register int    s;
  44.         char            bname[NBUFN];
  45.         char            fname[NFILEN];
  46.  
  47.         if ((s=mlreply("Visit file: ", fname, NFILEN)) != TRUE)
  48.                 return (s);
  49.         for (bp=bheadp; bp!=NULL; bp=bp->b_bufp) {
  50.                 if ((bp->b_flag&BFTEMP)==0 && strcmp(bp->b_fname, fname)==0) {
  51.                         if (--curbp->b_nwnd == 0) {
  52.                                 curbp->b_dotp  = curwp->w_dotp;
  53.                                 curbp->b_doto  = curwp->w_doto;
  54.                                 curbp->b_markp = curwp->w_markp;
  55.                                 curbp->b_marko = curwp->w_marko;
  56.                         }
  57.             prev_bp = curbp;
  58.                         curbp = bp;
  59.                         curwp->w_bufp  = bp;
  60.                         if (bp->b_nwnd++ == 0) {
  61.                                 curwp->w_dotp  = bp->b_dotp;
  62.                                 curwp->w_doto  = bp->b_doto;
  63.                                 curwp->w_markp = bp->b_markp;
  64.                                 curwp->w_marko = bp->b_marko;
  65.                         } else {
  66.                                 wp = wheadp;
  67.                                 while (wp != NULL) {
  68.                                         if (wp!=curwp && wp->w_bufp==bp) {
  69.                                                 curwp->w_dotp  = wp->w_dotp;
  70.                                                 curwp->w_doto  = wp->w_doto;
  71.                                                 curwp->w_markp = wp->w_markp;
  72.                                                 curwp->w_marko = wp->w_marko;
  73.                                                 break;
  74.                                         }
  75.                                         wp = wp->w_wndp;
  76.                                 }
  77.                         }
  78.                         lp = curwp->w_dotp;
  79.                         i = curwp->w_ntrows/2;
  80.                         while (i-- && lback(lp)!=curbp->b_linep)
  81.                                 lp = lback(lp);
  82.                         curwp->w_linep = lp;
  83.                         curwp->w_flag |= WFMODE|WFHARD;
  84.                         mlwrite("[Old buffer]");
  85.                         return (TRUE);
  86.                 }
  87.         }
  88.         makename(bname, fname);                 /* New buffer name.     */
  89.         while ((bp=bfind(bname, FALSE, 0)) != NULL) {
  90.                 s = mlreply("Buffer name: ", bname, NBUFN);
  91.                 if (s == ABORT)                 /* ^G to just quit      */
  92.                         return (s);
  93.                 if (s == FALSE) {               /* CR to clobber it     */
  94.                         makename(bname, fname);
  95.                         break;
  96.                 }
  97.         }
  98.         if (bp==NULL && (bp=bfind(bname, TRUE, 0))==NULL) {
  99.                 mlwrite("Cannot create buffer");
  100.                 return (FALSE);
  101.         }
  102.         if (--curbp->b_nwnd == 0) {             /* Undisplay.           */
  103.                 curbp->b_dotp = curwp->w_dotp;
  104.                 curbp->b_doto = curwp->w_doto;
  105.                 curbp->b_markp = curwp->w_markp;
  106.                 curbp->b_marko = curwp->w_marko;
  107.         }
  108.     prev_bp = curbp;
  109.         curbp = bp;                             /* Switch to it.        */
  110.         curwp->w_bufp = bp;
  111.         curbp->b_nwnd++;
  112.         return (readin(fname));                 /* Read it in.          */
  113. }
  114.  
  115. /*
  116.  * Read file "fname" into the current
  117.  * buffer, blowing away any text found there. Called
  118.  * by both the read and visit commands. Return the final
  119.  * status of the read. Also called by the mainline,
  120.  * to read in a file specified on the command line as
  121.  * an argument.
  122.  */
  123. readin(fname)
  124. char    fname[];
  125. {
  126.         register LINE   *lp1;
  127.         register LINE   *lp2;
  128.         register int    i;
  129.         register WINDOW *wp;
  130.         register BUFFER *bp;
  131.         register int    s;
  132.         register int    nbytes;
  133.         register int    nline;
  134.         char            line[NLINE];
  135.  
  136.         bp = curbp;                             /* Cheap.               */
  137.         if ((s=bclear(bp)) != TRUE)             /* Might be old.        */
  138.                 return (s);
  139.         bp->b_flag &= ~(BFTEMP|BFCHG);
  140.         strcpy(bp->b_fname, fname);
  141.         if ((s=ffropen(fname)) == FIOERR)       /* Hard file open.      */
  142.                 goto out;
  143.         if (s == FIOFNF) {                      /* File not found.      */
  144.                 mlwrite("[New file]");
  145.                 goto out;
  146.         }
  147.         mlwrite("[Reading file]");
  148.         nline = 0;
  149.         while ((s=ffgetline(line, NLINE)) == FIOSUC) {
  150.                 nbytes = strlen(line);
  151.                 if ((lp1=lalloc(nbytes)) == NULL) {
  152.                         s = FIOERR;             /* Keep message on the  */
  153.                         break;                  /* display.             */
  154.                 }
  155.                 lp2 = lback(curbp->b_linep);
  156.                 lp2->l_fp = lp1;
  157.                 lp1->l_fp = curbp->b_linep;
  158.                 lp1->l_bp = lp2;
  159.                 curbp->b_linep->l_bp = lp1;
  160.                 for (i=0; i<nbytes; ++i)
  161.                         lputc(lp1, i, line[i]);
  162.                 ++nline;
  163.         }
  164.         ffclose();                              /* Ignore errors.       */
  165.         if (s == FIOEOF) {                      /* Don't zap message!   */
  166.                 if (nline == 1)
  167.                         mlwrite("[Read 1 line]");
  168.                 else
  169.                         mlwrite("[Read %d lines]", nline);
  170.         }
  171. out:
  172.         for (wp=wheadp; wp!=NULL; wp=wp->w_wndp) {
  173.                 if (wp->w_bufp == curbp) {
  174.                         wp->w_linep = lforw(curbp->b_linep);
  175.                         wp->w_dotp  = lforw(curbp->b_linep);
  176.                         wp->w_doto  = 0;
  177.                         wp->w_markp = NULL;
  178.                         wp->w_marko = 0;
  179.                         wp->w_flag |= WFMODE|WFHARD;
  180.                 }
  181.         }
  182.         if (s == FIOERR)                        /* False if error.      */
  183.                 return (FALSE);
  184.         return (TRUE);
  185. }
  186.  
  187. /*
  188.  * Take a file name, and from it
  189.  * fabricate a buffer name. This routine knows
  190.  * about the syntax of file names on the target system.
  191.  * I suppose that this information could be put in
  192.  * a better place than a line of code.
  193.  */
  194. makename(bname, fname)
  195. char    bname[];
  196. char    fname[];
  197. {
  198.         register char   *cp1;
  199.         register char   *cp2;
  200.  
  201.         cp1 = &fname[0];
  202.         while (*cp1 != 0)
  203.                 ++cp1;
  204.  
  205. #if     AMIGA
  206.         while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='/')
  207.                 --cp1;
  208. #endif
  209. #if     VMS
  210.         while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!=']')
  211.                 --cp1;
  212. #endif
  213. #if     CPM
  214.         while (cp1!=&fname[0] && cp1[-1]!=':')
  215.                 --cp1;
  216. #endif
  217. #if     MSDOS
  218.         while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\')
  219.                 --cp1;
  220. #endif
  221. #if     V7
  222.         while (cp1!=&fname[0] && cp1[-1]!='/')
  223.                 --cp1;
  224. #endif
  225.         cp2 = &bname[0];
  226.         while (cp2!=&bname[NBUFN-1] && *cp1!=0 && *cp1!=';')
  227.                 *cp2++ = *cp1++;
  228.         *cp2 = 0;
  229. }
  230.  
  231. /*
  232.  * Ask for a file name, and write the
  233.  * contents of the current buffer to that file.
  234.  * Update the remembered file name and clear the
  235.  * buffer changed flag. This handling of file names
  236.  * is different from the earlier versions, and
  237.  * is more compatable with Gosling EMACS than
  238.  * with ITS EMACS. Bound to "C-X C-W".
  239.  */
  240. filewrite(f, n)
  241. {
  242.         register WINDOW *wp;
  243.         register int    s;
  244.         char            fname[NFILEN];
  245.  
  246.         if ((s=mlreply("Save As: ", fname, NFILEN)) != TRUE)
  247.                 return (s);
  248.         if ((s=writeout(fname)) == TRUE) {
  249.                 strcpy(curbp->b_fname, fname);
  250.                 curbp->b_flag &= ~BFCHG;
  251.                 wp = wheadp;                    /* Update mode lines.   */
  252.                 while (wp != NULL) {
  253.                         if (wp->w_bufp == curbp)
  254.                                 wp->w_flag |= WFMODE;
  255.                         wp = wp->w_wndp;
  256.                 }
  257.         }
  258.         return (s);
  259. }
  260.  
  261. /*
  262.  * Save the contents of the current
  263.  * buffer in its associatd file. No nothing
  264.  * if nothing has changed (this may be a bug, not a
  265.  * feature). Error if there is no remembered file
  266.  * name for the buffer. Bound to "C-X C-S". May
  267.  * get called by "C-Z".
  268.  */
  269. filesave(f, n)
  270. {
  271.         register WINDOW *wp;
  272.         register int    s;
  273.  
  274.         if ((curbp->b_flag&BFCHG) == 0)         /* Return, no changes.  */
  275.                 return (TRUE);
  276.         if (curbp->b_fname[0] == 0) {           /* Must have a name.    */
  277.                 mlwrite("No file name");
  278.                 return (FALSE);
  279.         }
  280.         if ((s=writeout(curbp->b_fname)) == TRUE) {
  281.                 curbp->b_flag &= ~BFCHG;
  282.                 wp = wheadp;                    /* Update mode lines.   */
  283.                 while (wp != NULL) {
  284.                         if (wp->w_bufp == curbp)
  285.                                 wp->w_flag |= WFMODE;
  286.                         wp = wp->w_wndp;
  287.                 }
  288.         }
  289.         return (s);
  290. }
  291.  
  292. /*
  293.  * This function performs the details of file
  294.  * writing. Uses the file management routines in the
  295.  * "fileio.c" package. The number of lines written is
  296.  * displayed. Sadly, it looks inside a LINE; provide
  297.  * a macro for this. Most of the grief is error
  298.  * checking of some sort.
  299.  * Updated to do reliable writing using a backup file whose name is
  300.  * fn with a 'O' appended.
  301.  */
  302. writeout(fn)
  303. char    *fn;
  304. {
  305.         register int    s;
  306.         register LINE   *lp;
  307.         register int    nline;
  308.     char fno[NFILEN+1], fnn[NFILEN+1];
  309.  
  310.     /* create file names */
  311.     strcpy( fnn, fn); strcat( fnn, "N"); /* for new file */
  312.     strcpy( fno, fn); strcat( fno, "O"); /* for old file */
  313.         if ((s=ffwopen(fnn)) != FIOSUC)          /* Open writes message. */
  314.                 return (FALSE);
  315.         lp = lforw(curbp->b_linep);             /* First line.          */
  316.         nline = 0;                              /* Number of lines.     */
  317.         while (lp != curbp->b_linep) {
  318.                 if ((s=ffputline(&lp->l_text[0], llength(lp))) != FIOSUC)
  319.                         break;
  320.                 ++nline;
  321.                 lp = lforw(lp);
  322.         }
  323.         if (s == FIOSUC) {                      /* No write error.      */
  324.                 s = ffclose();
  325.                 if (s == FIOSUC) {              /* No close error.      */
  326.                         if (nline == 1)
  327.                                 mlwrite("[Wrote 1 line]");
  328.                         else
  329.                                 mlwrite("[Wrote %d lines]", nline);
  330.                 }
  331.         } else                                  /* Ignore close error   */
  332.                 ffclose();                      /* if a write error.    */
  333.         if (s != FIOSUC)                        /* Some sort of error.  */
  334.                 return (FALSE);
  335.  
  336.     /* delete old back up and rename original to back up name */
  337.     unlink( fno);
  338.     if( access( fn, 0) == 0) /* original file exists */
  339.         if( rename( fn, fno) == -1) {
  340.         mlwrite("Old file rename failed");
  341.         return( FALSE);
  342.         }
  343.  
  344.     /* rename new to original name */
  345.     if( rename( fnn, fn) == -1) {
  346.         mlwrite("New file rename failed");
  347.         return( FALSE);
  348.     }
  349.  
  350.         return (TRUE);
  351. }
  352.  
  353. /*
  354.  * The command allows the user
  355.  * to modify the file name associated with
  356.  * the current buffer. It is like the "f" command
  357.  * in UNIX "ed". The operation is simple; just zap
  358.  * the name in the BUFFER structure, and mark the windows
  359.  * as needing an update. You can type a blank line at the
  360.  * prompt if you wish.
  361.  */
  362. filename(f, n)
  363. {
  364.         register WINDOW *wp;
  365.         register int    s;
  366.         char            fname[NFILEN];
  367.  
  368.         if ((s=mlreply("Name: ", fname, NFILEN)) == ABORT)
  369.                 return (s);
  370.         if (s == FALSE)
  371.                 strcpy(curbp->b_fname, "");
  372.         else
  373.                 strcpy(curbp->b_fname, fname);
  374.         wp = wheadp;                            /* Update mode lines.   */
  375.         while (wp != NULL) {
  376.                 if (wp->w_bufp == curbp)
  377.                         wp->w_flag |= WFMODE;
  378.                 wp = wp->w_wndp;
  379.         }
  380.         return (TRUE);
  381. }
  382.  
  383.